<?php

namespace App\Models;

use App\Http\Helper\PingppHelper;
use App\Http\Controllers\Weixin\WechatTemplateController;
use App\Http\Helper\UrlSchemaHelper;
use App\Http\Helper\YunXinServerAPI;

/**
 * Class Purchase
 * @property integer id ID
 * @property integer user_id 用户ID
 * @property integer seller_user_id 卖家ID
 * @property integer product_id 商品ID
 * @property float total 订单金额
 * @property integer is_payed 是否支付
 * @property string pay_method 支付方式
 * @property integer status 订单状态
 * @property integer return_status 退款状态
 * @property integer count 数量
 * @property integer sort 排序
 * @property string server_date 服务时间
 * @property string order_no 订单编号
 * @property string source 订单来源
 * @property string type 订单类型model_name
 * @property string cancel_name 取消订单操作员姓名
 * @property string cancel_note 取消订单原因
 * @property date pay_date 支付时间
 * @property date close_date 关闭时间
 * @property date accept_date 接单时间
 * @property integer coupon_number_id 用户优惠券id
 * @property date accept_end_date 接单结束时间
 * @property Product product 商品信息
 * @property User user 用户信息
 * @property User seller_user 卖家用户信息
 * @property Dynamic dynamic 动态
 * @property Vip vip 会员
 * @property CouponNumber couponNumber 优惠券
 */
class UserQuestion extends ApplicationModel
{
    protected $fillable = [
        'user_id',
        'question_id',
        'question',
        'theme_id',
        'theme',
        'pay_method',
        'to_user_id',
        'total',
        'is_payed',
        'order_no',
        'status',
        'tipping_type',
        'anonymous',
        'source',
        'pay_date',
        'close_date',
        'answer_date',
        'return_status',
        'count',
        'unread',
        'sort',
        'type',
        'remark',
        'device_type',
        'device_token',
        'os_version',
        'app_version',

    ];
    const PAY_WAITING = 0;//待支付
    const PAY_SUCCESS = 1;//已支付

    const STATUS_WAITING = 1;//等待付款
    const STATUS_QUESTIONER_CANCEL = 2;//提问者取消提问
    const STATUS_OVERTIME_PAYMENT = 3;//提问者超时未付款
    const STATUS_ANSWER_CANCEL = 4;//回答者取消订单
    const STATUS_ANSWER_ACCEPT_WAITING = 5;//等待回答者回答
    const STATUS_ANSWER_ACCEPT = 6;//回答者回答
    const STATUS_ANSWER_OVERTIME = 7;//回答者超时未接单
    const STATUS_FINISH = 8;//订单完成
    const STATUS_ADMIN_CANCEL = 9;//后台强制取消

    const STATUS_REFUND_AUDIT = 11;  //退款申请审核中
    const STATUS_REFUND_REJECT = 13;  //退款申请拒绝
    const STATUS_REFUND_PASS = 12;  //退款申请通过
    const STATUS_REFUND_WITHDRAW = 14;  //退款申请撤销


    const STATUS_SELLER_CANCEL_AUDIT = 16;  //卖家取消订单 等待买家通过
    const STATUS_SELLER_CANCEL_PASS = 17;  //卖家取消订单 买家通过



    const STATUS_REFUND_ACCEPT_OVERTIME = 15;  //退款申请超时 卖家自动通过
    const STATUS_SELLER_CANCEL_OVERTIME = 18;  //卖家取消订单申请 超时通过 买家通过


    const RETURN_STATUS_WAITING = 1;//退款中
    const RETURN_STATUS_SUCCESS = 2;//退款成功

    const SOURCE_CHUGE = 'chuge_activity';
    const SOURCE_APP = 'app';

    public function pay_status_desc()
    {
        switch ($this->status) {
            case static::STATUS_WAITING:
                return "等待付款";
                break;
            case static::STATUS_ANSWER_ACCEPT_WAITING:
                return "等待回答";
                break;
            case static::STATUS_ANSWER_CANCEL:
                return "回答者取消";
                break;

            default;
        }
    }

    public function return_status_desc()
    {
        if ($this->return_status == static::RETURN_STATUS_WAITING)
        {
            return "退款中";
        }
        if ($this->return_status == static::RETURN_STATUS_SUCCESS)
        {
            return "退款成功";
        }
    }



    public function scopeChuge($query)
    {
        return $query->where('source', static::SOURCE_CHUGE);
    }



    public function scopeFinish($query){
        return $query->where('status',8);
    }
    public function scopeNotFinish($query)
    {
        return $query->whereIn('status',[static::STATUS_WAITING,static::STATUS_ANSWER_ACCEPT_WAITING]);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function answer_user()
    {
        return $this->belongsTo(User::class, 'to_user_id', 'id');
    }



    public function pay_method_desc()
    {
        if ($this->is_payed == static::PAY_WAITING)
        {
            return '未支付';
        }
        if ($this->pay_method == 'wx')
        {
            return "微信";
        }
        if ($this->pay_method == 'wx_pub')
        {
            return "微信公众号";
        }
        if ($this->pay_method == 'alipay')
        {
            return "支付宝支付";
        }
    }

    public function payment()
    {
        return $this->hasOne(Payment::class);
    }

    public function refund()
    {
        return $this->hasOne(Refund::class);
    }

    public function status_str()
    {
        switch ($this->status) {
            case 1:
                return 'PAY_WAITING';
            case 2:
            case 3:
            case 4:
            case 7:
            case 9:
            case 10:
                return 'CLOSE';
            case 11:
                return 'REFUND_AUDITING';
            case 12:
                return 'REFUND_PASS';
            case 13:
                return 'REFUND_REJECT';
            case 14:
                return 'REFUND_WITHDRAW';
            case 15:
                return 'REFUND_OVERTIME_PASS';
            case 5:
                return 'WAITING_ANSWER';
            case 6:
                return 'ACCEPT';
            case 8:
                return 'FINISH';

        }
    }
















}
